<?php
/* return facebook Object */
function InitFacebookObject()
{
  $facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_APP_SECRET,
  ));

  return $facebook;
}

function InitFacebookUser()
{
  $facebook = InitFacebookObject();
  $user = $facebook->getUser();

  return $user;
}

function GetFacebookUserProfile()
{
  $facebook = InitFacebookObject();
  $user = InitFacebookUser();
  if ($user)
  {
    try {
      // Proceed knowing you have a logged in user who's authenticated.
      $user_profile = $facebook->api('/me');
    } 
    catch (FacebookApiException $e)
    {
      error_log($e);
      $user = null;
    }
  }
  
  return $user_profile;
}


function PrintBeginContent()
{
  print '</head><body>';
}

function PrintEndContent()
{
  print '</body></html>';
}

function PrintTopMenu()
{
  $headerItems = array();
  $classes = array('agencia', 'servisos', 'portfolio', 'clientes', 'contato');
  $categories  = Category::LoadCategories();
  $index=0;
  foreach($categories as $category)
  {
    $headerItems[$index] = array(
      'title' => $category->title,
      'href'  => URL::to_route('games', $category->name),
      'class' => $classes[$index]
    );
    $index++;
  }
  
  ?>

  <div class="top-menu-wrap">
      <div id="menu">
        <a id="logo" href="<?php print URL::base();?>"></a>
        <div class="global-nav">
          <input id="search-box" type="text" name="search" value="Search">
          <ul class="login">
            <?php
            
              print '<li>Have an account? <a href="/login">Sign In</a> or <a href="/register">Sign Up</a></li>';
            //else
              //var_dump($_SESSION);
              //print '<li>' . $_SESSION['userName'] . '</li>';
            ?>
            <li><a class="facebook-icon"></a></li>
            <li><a class="twitter-icon"></a></li>
            <li><a class="google-icon"></a></li>
          </ul>
        </div>
        <ul>       
        <?php
          foreach ($headerItems as $item)
          {
            print '<li>' . HTML::link($item['href'], $item['title'], array('class' => 'label-title ' . $item['class'])) . '</li>';
          }
        ?>
        </ul>
      </div>   
    </div>
<?php
}

function PrintFooterPage()
{
?>
  <div id="footer">
    <div class="footer-wrap">
      <div id="copyright" class="left">Copyright © 2013 Bencheci</div>
      <div class="right" id="links">
        <a href="/about">About us</a><span> |</span>
        <a href="/contacts">Contacts</a><span> |</span>
        <a href="/description">Description</a>
      </div>
    </div>
  </div>
<?php  
}

function RenderLoginForm()
{
  print Form::open('login', 'POST', array('class' => 'form'));
  print '<p>'
          . Form::label('email', 'Email')
          . Form::email('email')
          . '</p>'
        . '<p>'
          . Form::label('password', 'Password')
          . Form::password('password')
        . '</p>'
        . '<div class="clear"></div>'
        . '<p>' . Form::submit('Login', array('id' => 'btnLogin', 'class' => 'btn btn-primary')) . '</p>';
    print Form::close();
}

function RenderSignUpForm()
{
  print Form::open('register', 'POST', array('class' => 'form'));
  print '<p>'
          . Form::label('name', 'Name')
          . Form::text('name')
        . '</p>'
        . '<p>'
          . Form::label('email', 'Email')
          . Form::email('email')
        . '</p>'
        . '<p>'
          . Form::label('password', 'Password')
          . Form::password('password')
        . '</p>'
        . '<p>'
          . Form::label('confirm', 'Confirmation')
          . Form::password('confirm-password')
        . '</p>'
        . '<div class="clear"></div>'
        . '<p>' . Form::submit('Sign Up', array('id' => 'btnLogin', 'class' => 'btn btn-primary')) . '</p>';
    print Form::close();
}

function RegisterFacebookUser($user_profile)
{
  $user = new User();
  $user->nickname = $user_profile['name'];
  $user->facebookId = $user_profile['id'];
  $user->email = $user_profile['email'];
  $user->password = 'dsfsdf';
  $user->Save();
}

function PrintGameDescriptionBlock($game)
{
  $shareGameUrl = FACEBOOK_SHARE_LINK . SITE_NAME . $game->GenerateGameUrl();
  $socialIcons = array(
      'facebook' => array(
          'onclick' => 'window.open("' . $shareGameUrl . '", "' . FACEBOOK_SHARE_LINK . '", "width=626, height=436"); return false;',
          'title'   => 'Share on Facebook'
      ),
      'twitter' => array(
          'onclick' => 'return false',
          'title'   => 'Share on Twitter'
      )
  );
  
?>
  <p class="title">Rating</p> 
  <?php RenderRatingStars($game, 30); ?>
  <p><?php print 'Rating <span class="rating">' . $game->rating . '</span> / <b>5</b>' . ' (' . $game->ratingCount . ' votes cast)'; ?></p>
  <p><?php print '<strong>' . $game->views . '</strong>' ?> Views</p>
  <p class="title">Play with friends</p>
    <?php
    print '<ul id="social-icons">';
    foreach($socialIcons as $key => $attributes)
    {
      print '<li class="' . $key . '">' . HTML::link('#', '', $attributes) . '</li>';
    }
    print '</ul>';
    ?>
<?php
}

function RenderRatingStars($game, $size = 20, $class = null)
{
  $width = ($game->rating / 5) * 100;
?>
    <div class="ratingstars <?php print $class; ?>">
    <div class="ratepost gdsr-oxygen gdsr-size-<?php print $size?>" id="article_rater_1146">
      <div class="starsbar gdsr-size-<?php print $size?>">
        <div class="gdouter gdheight">
          <div class="gdinner gdheight" <?php print ($width > 0) ? 'style="width:' . $width . '%;"' : '' ?> id="gdr_vote_a1146"></div>
          <div class="gdsr_rating_as" id="gdr_stars_a1146">
            <a rel="nofollow" class="s5" title="5 / 5"></a>
            <a rel="nofollow" class="s4" title="4 / 5"></a>
            <a rel="nofollow" class="s3" title="3 / 5"></a>
            <a rel="nofollow" class="s2" title="2 / 5"></a>
            <a rel="nofollow" class="s1" title="1 / 5"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
  
<?php
}

function LoadFacebookCommentScript()
{
?>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=<?php print FACEBOOK_APP_ID;?>";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<?php
}
?>
